# 构建阶段
FROM golang:1.24-alpine AS builder

WORKDIR /myapp

# 复制依赖文件
COPY go.mod go.sum ./

# 配置 Go 环境
RUN go env -w GO111MODULE=on \
    && go env -w GOPROXY=https://goproxy.cn,direct \
    && go env -w CGO_ENABLED=0 \
    && go env

# 下载依赖
RUN go mod download || go mod tidy

# 复制源代码
COPY . .

# 构建可执行文件
RUN go build -o app .

# 运行阶段
FROM alpine:latest

WORKDIR /home/qingzhou

# 创建 qingzhou 用户
RUN adduser -D qingzhou

# 创建日志目录并设置权限
RUN mkdir /log && chown qingzhou:qingzhou /log

# 复制可执行文件
COPY --from=builder /myapp/app .
COPY --from=builder /myapp/config.yaml .

# 设置可执行文件权限
RUN chown qingzhou:qingzhou app

# 切换到 qingzhou 用户
USER qingzhou

# 暴露端口
EXPOSE 8888

# 启动命令
CMD ["./app"]
